﻿using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CameraControl
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                if(videoDevices.Count==0)
                {
                    MessageBox.Show("没有检测到本地硬件");
                    return;
                }
                //foreach(FilterInfo d in videoDevices)
                //{
                //    VideoCaptureDevice videoSource = new VideoCaptureDevice(d.MonikerString);
                //    //videoSource.DesiredFrameSize = new System.Drawing.Size(320, 240);
                //    //videoSource.DesiredFrameRate = 1;

                //    CameraViedo1.VideoSource = videoSource;
                //    CameraViedo1.Start();
                //}
                for(int i=0;i<videoDevices.Count;i++)
                {
                    VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[i].MonikerString);
                    if(i==0)
                    {
                        CameraViedo1.VideoSource = videoSource;
                        CameraViedo1.Start();
                    }
                    else if(i==1)
                    {
                        CameraViedo2.VideoSource = videoSource;
                        CameraViedo2.Start();
                    }
                    else if (i == 2)
                    {
                        CameraViedo3.VideoSource = videoSource;
                        CameraViedo3.Start();
                    }
                }

            }
            catch (Exception ex)
            {

            }
        }

        private void btnPhoto1_Click(object sender, EventArgs e)
        {
            try
            {
                if (CameraViedo1.IsRunning)
                {
                    //BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    //                videoSourcePlayer.GetCurrentVideoFrame().GetHbitmap(),
                    //                IntPtr.Zero,
                    //                 Int32Rect.Empty,
                    //                BitmapSizeOptions.FromEmptyOptions());
                    //PngBitmapEncoder pE = new PngBitmapEncoder();
                    //pE.Frames.Add(BitmapFrame.Create(bitmapSource));
                    //string picName = GetImagePath() + "\\" + "xiaosy" + ".jpg";
                    //if (File.Exists(picName))
                    //{
                    //    File.Delete(picName);
                    //}
                    //using (Stream stream = File.Create(picName))
                    //{
                    //    pE.Save(stream);
                    //}
                    ////拍照完成后关摄像头并刷新同时关窗体
                    //if (videoSourcePlayer != null && videoSourcePlayer.IsRunning)
                    //{
                    //    videoSourcePlayer.SignalToStop();
                    //    videoSourcePlayer.WaitForStop();
                    //}
                    Bitmap img = CameraViedo1.GetCurrentVideoFrame();
                    this.pictureBox1.Image = img;
                    //string picName = @"D\:temp\" + Guid.NewGuid().ToString() + ".bmp";
                    //img.Save(picName);


                    //this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("摄像头异常：" + ex.Message);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.FileName = "画图";
            save.Filter = "(.jpg)|*.jpg";

            if (save.ShowDialog() == DialogResult.OK)
            {
                Bitmap bit = new Bitmap(pictureBox1.ClientRectangle.Width, pictureBox1.ClientRectangle.Height);
                pictureBox1.DrawToBitmap(bit, pictureBox1.ClientRectangle);
                //string picName = @"D\:temp\" + Guid.NewGuid().ToString() + ".bmp";
                bit.Save(save.FileName);
            }
            //Bitmap bit = new Bitmap(pictureBox1.ClientRectangle.Width, pictureBox1.ClientRectangle.Height);
            //pictureBox1.DrawToBitmap(bit, pictureBox1.ClientRectangle);
            //string picName = @"D\:temp\" + Guid.NewGuid().ToString() + ".bmp";
            //bit.Save(picName);
        }

        private void btnPhoto3_Click(object sender, EventArgs e)
        {
            try
            {
                if (CameraViedo3.IsRunning)
                {
                    Bitmap img = CameraViedo3.GetCurrentVideoFrame();
                    this.pictureBox1.Image = img;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("摄像头异常：" + ex.Message);
            }
        }

        private void btnPhoto2_Click(object sender, EventArgs e)
        {
            try
            {
                if (CameraViedo2.IsRunning)
                {
                    Bitmap img = CameraViedo2.GetCurrentVideoFrame();
                    this.pictureBox1.Image = img;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("摄像头异常：" + ex.Message);
            }
        }
    }
}
